package com.ebt.m.view;

import android.app.AlertDialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.utils.UIHelper;

/**
 * Created by archer.qi on 2016/7/1.
 */
public class InputDialog {

    private Context mContext;

    private View contentView;
    private TextView mTvTitle;
    private Button mBtnPositive, mBtnNegative;
    private EditText mEditText;

    private String mStrTitle;
    private String mStrPositive = "取消";
    private String mStrNegative = "确定";

    private AlertDialog mDialog;

    public InputDialog(Context context) {
        this.mContext = context;
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        mDialog = new AlertDialog.Builder(context).create();
        mDialog.show();
        mDialog.getWindow().setContentView(setupContentView());
        mDialog.setCanceledOnTouchOutside(false);//点击外部不能消失
        mDialog.getWindow().setLayout(dm.widthPixels * 4 / 5, UIHelper.dip2px(mContext, 200));
        WindowManager.LayoutParams p = mDialog.getWindow().getAttributes();
        p.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE;
        p.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        mDialog.getWindow().setAttributes(p);
    }

    private View setupContentView() {
        LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = mInflater.inflate(R.layout.dialog_roundcorner, null);

        mTvTitle = (TextView) contentView.findViewById(R.id.Dialog_RoundCorner_Title);
        mEditText = (EditText) contentView.findViewById(R.id.Dialog_RoundCorner_EditText);
        mBtnPositive = (Button) contentView.findViewById(R.id.Dialog_RoundCorner_PositiveButton);
        mBtnNegative = (Button) contentView.findViewById(R.id.Dialog_RoundCorner_NagetiveButton);

        return contentView;
    }


    public void setTitle(String text) {
        mStrTitle = text;
        if (mStrTitle != null && mTvTitle != null) {
            mTvTitle.setText(mStrTitle);
        }
    }

    public void setEdit(String text) {
        if (mEditText != null) {
            if (text != null) {
                mEditText.setText(text);
                mEditText.setSelection(mEditText.getText().length());
            }
            mEditText.requestFocus();
        }
    }

    public String getEditString() {
        return mEditText.getText().toString().trim();
    }

    public void setPositiveButton(String text, View.OnClickListener listener) {
        mStrPositive = text;
        if (mBtnPositive != null) {
            if (null == mStrPositive && "".equals(mStrPositive)) {
                mStrPositive = "确定";
            }
            mBtnPositive.setText(mStrPositive);
            if (listener != null) {
                mBtnPositive.setOnClickListener(listener);
            }
        }
    }


    public void setNegativeButton(String text, View.OnClickListener listener) {
        mStrNegative = text;
        if (mBtnNegative != null) {
            if (null == mStrNegative && "".equals(mStrNegative)) {
                mStrNegative = "取消";
            }
            mBtnNegative.setText(mStrNegative);
            if (listener != null) {
                mBtnNegative.setOnClickListener(listener);
            }
        }
    }

    public AlertDialog getDialog() {
        return mDialog;
    }

    public String getTitle() {
        return mStrTitle;
    }

    public boolean isShowing() {
        return mDialog.isShowing();
    }

    public void show() {
        if (!isShowing()) {
            mDialog.show();
        }
    }

    public void dismiss() {
        if (isShowing()) {
            mDialog.dismiss();
        }
    }

}
